<?php

namespace IDock\Insurance\CHAC\Response;

use IDock\Http\Response;
use IDock\Printer\VehicleActualValuePrinter;
use IDock\Visitor\VehicleActualValueVisitor;

class CVehicleActualValueResponse extends AllCHACResponse
{
    /**
     * @var VehicleActualValueVisitor $visitor
     */
    public $visitor;

    public function setVisitor($visitor): void
    {
        $this->visitor = $visitor;
    }

    public function printerInit(): Response
    {
        $this->printer = new VehicleActualValuePrinter();

        $vehiclePrice = $this->visitor->vehiclePrice;

        $firstRegisterDate = $this->visitor->firstRegisterDate;
        $startDate = date('Y-m-d', strtotime($this->visitor->startDate));
        $monthDiff = $this->monthDiff($firstRegisterDate, $startDate);
        //默认
        $this->actualRate();

        $actualValue = $vehiclePrice * (1 - $monthDiff * $this->printer->actualRate);

        $this->printer->actualValue = $actualValue;

        $actualValueMostMin = $vehiclePrice * 0.2;

        $this->printer->actualValueMax = round(max($actualValue * 1.3,$actualValueMostMin),2);

        $this->printer->actualValueMin = round(max($actualValue * 0.7, $actualValueMostMin),2);

        $this->printer->actualValue = round(max($this->printer->actualValue, $this->printer->actualValueMin),2);

        $this->printer->actualRate =  (string)round($this->printer->actualRate * 100,2);

        return $this;
    }

    /**
     * 折旧率计算
     * @return void
     */
    private function actualRate() {
        $carKindCode = $this->visitor->carKindCode;
        $useNatureCode = $this->visitor->useNatureCode;
        $causeType = $this->visitor->causeType;
        $vehicleSeat = $this->visitor->vehicleSeat;
        $vehicleTonnage = $this->visitor->vehicleTonnage;
        $energyType = $this->visitor->energyType;

        $vehiclePrice = $this->visitor->vehiclePrice;

        //非新能源
        if (!$this->visitor->isNewEnergy) {

            //9座（含9座）客车
            if ($vehicleSeat <= 9 and $carKindCode == '100') {
                $this->printer->actualRate = '0.0060';
                return;
            }
            //6吨载货汽年
            if ($vehicleTonnage > 6000) {
                $this->printer->actualRate = '0.0120';
                return;
            }

            //非营运载客汽车
            if ($useNatureCode == '01' and  ($carKindCode == '210' or $carKindCode == '200')) {
                $this->printer->actualRate = '0.0060';
                return;
            }

            //出租
            if ($causeType == '004') {
                $this->printer->actualRate = '0.0120';
                return;
            }

            $this->printer->actualRate = '0.0090';
            return;

        } else {
        //新能源
            //9座（含9座）客车
            if ($vehicleSeat <= 9 and $carKindCode == '100') {
                //纯电动
                if ($energyType == 'E02') {

                    if ($vehiclePrice < 100000) {
                        $this->printer->actualRate = '0.0082';
                    }
                    if ($vehiclePrice >= 100000 and $vehiclePrice < 200000) {
                        $this->printer->actualRate = '0.0077';
                    }
                    if ($vehiclePrice >= 200000 and $vehiclePrice < 300000) {
                        $this->printer->actualRate = '0.0072';
                    }
                    if ($vehiclePrice >= 300000) {
                        $this->printer->actualRate = '0.0068';
                    }

                    return;
                }

                //9座（含9座）客车，燃料电池、插电式混合动力
                if ($energyType == 'E03' or $energyType == 'E04') {
                    $this->printer->actualRate = '0.0063';
                    return;
                }
            }
            //低速货车
            if ($carKindCode == '210') {
                $this->printer->actualRate = '0.0110';
                return;
            }


            $this->printer->actualRate = '0.0090';
            return;
        }
    }

    /**
     * 相差月份
     * @param $firstRegisterDate
     * @param $startDate
     * @return float|int|mixed|string
     */
    private function monthDiff($firstRegisterDate, $startDate)
    {
        $firstRegisterDate = explode('-', $firstRegisterDate);

        $startDate = explode('-', $startDate);

        $DYear = $startDate[0] - $firstRegisterDate[0];
        $DMonth = $startDate[1] - $firstRegisterDate[1];
        $DDay = $startDate[2] - $firstRegisterDate[2];

        $monthDiff = $DYear * 12 + $DMonth;

        if ($DDay < 0) {
            $monthDiff -= 1;
        }

        return $monthDiff;
    }
}
